package gjw.jimven.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AlertDialog.Builder;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.telephony.PhoneNumberUtils;
import android.widget.Button;
import android.widget.Toast;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import gjw.jimven.app.JimvenApplication;

public class CommonUtils {

	/** 
     * 调起系统发短信功能 
     * @param phoneNumber 
     * @param message 
     */  
    public static void doSendSMSTo(Context context,String phoneNumber,String message){  
        if(PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)){  
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:"+phoneNumber));            
            intent.putExtra("sms_body", message);            
            context.startActivity(intent);  
        }  
    }
	/** 
     * 读取字符串第i次出现特定符号的位置 
     * @param string 
     * @param i 
     * @return 
     */  
    public static int getCharacterPosition(String string ,int i,String character){  
        //这里是获取"/"符号的位置  
       // Matcher slashMatcher = Pattern.compile("/").matcher(string);  
         Matcher slashMatcher = Pattern.compile(character).matcher(string);  
        int mIdx = 0;  
        while(slashMatcher.find()) {  
           mIdx++;  
           //当"/"符号第三次出现的位置  
           if(mIdx == i){  
              break;  
           }  
        }  
        return slashMatcher.start();  
     }
	
	public static String getMsgTime(long time){
		long start = System.currentTimeMillis();
		long t = start - time;
		if(t<0){
			//因为用户的时间可能不准
			if(t<-1200000){
				Toast.makeText(JimvenApplication.getContext(), "请修正好时间", Toast.LENGTH_SHORT).show();
			}else{
				return "刚刚";
			}
//			throw new IllegalArgumentException("出入的时间戳不能大于当前时间");
		}
		long hours = t/3600000;
		if(hours==0){
			return "刚刚";
		}else if(hours<24){
			return hours+"小时前";
		}
		int days = (int) (hours/24);
		if(days<30){
			return days+"天前";
		}
		int month = days/30;
		if(month<12){
			return month+"月前";
		}
		int years = month/12;
		return years+"年前";
	}
	
	
	/**
	 * 判断是否是手机号码
	 * @param number 电话号码
	 * @return
	 */
	public static boolean isMobileNO(String number){  
//		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0,5-9]))[-| ]?\\d{4}[-| ]?\\d{4}$");
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0,5-9]))\\d{8}$");  
		Matcher m = p.matcher(number);  
		System.out.println(m.matches()+"---");  
		return m.matches();  
	}
	
	/**
	 * 判断是否是银行卡号码
	 * @param number 银行卡号码
	 * @return
	 */
	public static boolean isBankNO(String number){  
		Pattern p = Pattern.compile("^(\\d{16}|\\d{19})$");  
		Matcher m = p.matcher(number);  
		System.out.println(m.matches()+"---");  
		return m.matches();  
	}
	
	/**
	 * 创建AlertDialog
	 * @param context
	 * @param title
	 * @param message
	 * @param onSuccess
	 */
	public static void createDialog(Context context,String title,String message,OnClickListener onSuccess){
		Builder builder = new Builder(context);
		builder.setTitle(title)
		.setMessage(message)
		.setNegativeButton("取消", null)
		.setPositiveButton("确定", onSuccess)
		.setCancelable(false)
		.create()
		.show();
	}
	
	public static void changeButtonBackground(Context context,Button button,int drawableId,boolean enabled){
		button.setEnabled(enabled);
		Drawable drawable = context.getResources().getDrawable(drawableId);
		button.setBackground(drawable);
	}
	
	/**
     *判断当前应用程序处于前台还是后台
     */
    public static boolean isApplicationBroughtToBackground(final Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(context.getPackageName())) {
                return true;
            }
        }
        return false;
    }
	
}
